package com.byzk.p2p.admin.user.controller;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.user.dto.UserAuth;
import com.byzk.p2p.admin.user.service.UserAuthService;

@Controller
@RequestMapping("/admin/userAuth")
public class UserAuthController extends AdminBaseController {
	
	@Autowired
	private UserAuthService userAuthService;
	
	/**
	 * 分页查询用户认证信息
	 * @param model 视图对象
	 * @param index 当前页
	 * @param limit 每页显示条数
	 * @param username 用户名
	 * @param authName 认证显示名
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/list")
	public String listUserAuth(Model model,
			   @RequestParam(value="index",required=false,defaultValue="1") int index,
			   @RequestParam(value="limit",required=false,defaultValue="10") int limit,
			   @RequestParam(value="username",required=false) String username,
			   @RequestParam(value="authName",required=false) String authName) throws UnsupportedEncodingException{
		UserAuth userAuth = new UserAuth();
		userAuth.setIndex(index);
		userAuth.setLimit(limit);
		if (StringUtils.isNotBlank(username)) {
			username=java.net.URLDecoder.decode(username , "UTF-8");
			userAuth.setMatchUser(username);
			model.addAttribute("username", username);
		}
		if (StringUtils.isNotBlank(authName)) {
			userAuth.setAuthName(authName);
			model.addAttribute("authName", authName);
		}
		PageInfo<UserAuth> pageInfo=userAuthService.listUserAuth(userAuth);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 根据id认证  相关记录
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/show")
	public String showUserAuthById(Model model,@RequestParam(value="id") Integer id){
		UserAuth userAuth = userAuthService.getUserAuthById(id);
		model.addAttribute("userAuth", userAuth);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	
	/**
	 * 新增，修改认证记录 方法
	 * @param scoreRecord
	 * @return
	 */
	@RequestMapping("/update")
	public String updateUserAuth(UserAuth userAuth){
		try {
			Integer i = userAuthService.saveOrUpdateUserAuth(userAuth);
			if(i>0){
				super.printJSON(new JsonMsg(-1,"操作成功",i));
			}else{
				super.printJSON(new JsonMsg(0,"操作失败",""));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
